package org.example.cart.controller;

import org.example.cart.service.CartService;
import org.example.cart.vo.*;
import org.example.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小杰_cc
 * @version 1.0
 */

@RestController
@RequestMapping("/cart/front")
public class CartFrontController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物
     */
    @PostMapping("/add")
    public R<?> addCart(@Validated @RequestBody CartRestVo cartRestVo){
        cartService.addCartSku(cartRestVo);
        return R.ok();
    }

    /**
     * 获取购物车
     */
    @GetMapping("/list")
    public R<CartVo> queryCart(){
        CartVo cartVo = cartService.queryCartItem();
        return R.ok(cartVo);
    }

    /**
     * 修改购物车信息
     */
    @PutMapping("/update")
    public R<?> editCart(@Validated @RequestBody CartRestVo cartRestVo){
        cartService.updateCart(cartRestVo);
        return R.ok();
    }

    /**
     * 购物车全选
     * @param selected
     * @return
     */
    @PutMapping("/updates")
    public R<?> editCartAll(@RequestBody Boolean selected){
        cartService.updateCartAll(selected);
        return R.ok();
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/deletes")
    public R<?> deletes(@RequestBody List<Long> skuIds){
        cartService.deleteCartItems(skuIds);
        return R.ok();
    }

    /**
     * 获取购物车数量（不含单个商品的个数）
     */
    @GetMapping("/count")
    public R<Long> getCartCount(){
        Long count = cartService.queryCartCount();
        return R.ok(count);
    }

    /**
     * 获取选中的购物车列表
     * @return
     */
    @GetMapping("/selected")
    public R<List<CartItemVo>> selected(){
        List<CartItemVo> cartItemVos =  cartService.querySelected();
        return R.ok(cartItemVos);
    }

    @PostMapping("/move/collect")
    public R<?> moveCollect(@RequestBody MoveCollectVo moveCollectVo){
        return cartService.moveCollect(moveCollectVo);
    }
}